package com.ti.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @description: com.ti.demo.controller
 * @Author Triagen
 * @Date 2025/7/18 10:53
 */
@RestController
public class ChatController {

    @Qualifier("chatClient")
    @Autowired
    private ChatClient chatClient;

    @GetMapping("/chat")
    Flux<String> openaiChat(@RequestParam(value = "conversationId", defaultValue = "chat") String conversationId,
                            @RequestParam(value = "userInput", defaultValue = "讲个笑话") String userInput) {
        return this.chatClient.prompt()
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId))
                .user(userInput)
                .stream()
                .content();
    }
}
